package o2o.thridpost.dada.controller;

import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import o2o.config.thirdpost.dada.DadaConfig;
import o2o.config.thirdpost.dada.DadaRequestClient;
import o2o.thridpost.dada.client.DadaApiResponse;
import o2o.thridpost.dada.client.DadaRequestParam;
import o2o.thridpost.dada.config.UrlConstant;
import o2o.thridpost.dada.model.to.*;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @author sunyufan deporation@qq.com
 * @date 2021/5/6 14:35
 * @description: ControllerTest
 */
@Slf4j
public class ControllerTest {

    @Autowired
    private DadaRequestClient requestClient;

    @Autowired
    private Gson gson;

    public ControllerTest() {
        this.gson = new Gson();
    }

    @Test
    public void addOrder() {

        String json = "{\n" +
                "    \"shop_no\": \"11047059\",\n" +
                "    \"origin_id\": \"123457000\",\n" +
                "    \"city_code\": \"021\",\n" +
                "    \"cargo_price\": 100,\n" +
                "    \"is_prepay\": 0,\n" +
                "    \"receiver_name\": \"测试\",\n" +
                "    \"receiver_address\": \"隆宇大厦\",\n" +
                "    \"receiver_lat\": 31.228623,\n" +
                "    \"receiver_lng\": 121.587172,\n" +
                "    \"receiver_phone\": \"13622219090\",\n" +
                "    \"callback\": \"http://newopen.qa.imdada.cn\"\n" +
                "}";
        AddDadaOrderTO addDadaOrderTO = gson.fromJson(json, AddDadaOrderTO.class);
        log.info("接收参数:" + addDadaOrderTO);
        DadaRequestParam<AddDadaOrderTO> requestParam = new DadaRequestParam<>(addDadaOrderTO, requestClient.getAppConfig());
        DadaApiResponse apiResponse = requestClient.callRpc(requestParam, UrlConstant.RE_ADD_ORDER_URL);

        log.info(apiResponse.toString());

    }

    @Test
    public void cancelOrder() {
        String json = "{\n" +
                "    \"order_id\": \"123457000\",\n" +
                "    \"reason\": \"5\"\n" +
                "}";
        CancelDadaOrderTO cancelDadaOrderTO = gson.fromJson(json, CancelDadaOrderTO.class);
        log.info("接收参数:" + cancelDadaOrderTO);
        DadaRequestParam<CancelDadaOrderTO> requestParam = new DadaRequestParam<>(cancelDadaOrderTO, requestClient.getAppConfig());
        DadaApiResponse apiResponse = requestClient.callRpc(requestParam, UrlConstant.CANCEL_ORDER_URL);

        log.info(apiResponse.toString());
    }

    @Test
    public void queryDeliverFee() {
        String json = "{\n" +
                "    \"shop_no\": \"11047059\",\n" +
                "    \"origin_id\": \"123457000\",\n" +
                "    \"city_code\": \"021\",\n" +
                "    \"cargo_price\": 100,\n" +
                "    \"is_prepay\": 0,\n" +
                "    \"receiver_name\": \"测试\",\n" +
                "    \"receiver_address\": \"隆宇大厦\",\n" +
                "    \"receiver_lat\": 31.228623,\n" +
                "    \"receiver_lng\": 121.587172,\n" +
                "    \"receiver_phone\": \"13622219090\",\n" +
                "    \"callback\": \"http://newopen.qa.imdada.cn\"\n" +
                "}";
        QueryDeliverFeeTO queryDeliverFeeTO = gson.fromJson(json, QueryDeliverFeeTO.class);
        log.info("接收参数:" + queryDeliverFeeTO);
        DadaRequestParam<QueryDeliverFeeTO> requestParam = new DadaRequestParam<>(queryDeliverFeeTO, requestClient.getAppConfig());
        DadaApiResponse apiResponse = requestClient.callRpc(requestParam, UrlConstant.QUERY_DELIVER_FEE);

        System.out.println(apiResponse.getResult().toString());

        HashMap<String, String> result = gson.fromJson(apiResponse.getResult().toString(), HashMap.class);
        if (result.containsKey("deliveryNo")) {
            Map<String, String> param = new HashMap<>(1);
            param.put("deliveryNo", result.get("deliveryNo"));
            DadaRequestParam<Map<String, String>> requestParam1 = new DadaRequestParam<>(param, requestClient.getAppConfig());
            apiResponse = requestClient.callRpc(requestParam1, UrlConstant.ADD_AFTER_QUERY);
            log.info(apiResponse.toString());
        }
    }

    @Test
    public void queryOrder() {
        Map<String, String> param = new HashMap<>(1);
        param.put("order_id", "123457000");

        DadaRequestParam<Map<String, String>> requestParam = new DadaRequestParam<>(param, requestClient.getAppConfig());
        DadaApiResponse apiResponse = requestClient.callRpc(requestParam, UrlConstant.QUERY_ORDER);

        log.info(apiResponse.toString());
    }

    @Test
    public void queryCancelReason() {
        DadaRequestParam<String> requestParam = new DadaRequestParam<>("", requestClient.getAppConfig());

        DadaApiResponse apiResponse = requestClient.callRpc(requestParam, UrlConstant.CANCEL_REASON);

        log.info(apiResponse.toString());
    }

    @Test
    public void queryCityCode() {

        DadaRequestParam<String> requestParam = new DadaRequestParam<>("", requestClient.getAppConfig());

        DadaApiResponse apiResponse = requestClient.callRpc(requestParam, UrlConstant.CITY_CODE_URL);

        log.info(apiResponse.toString());
    }

    @Test
    public void addMerchant() {
        String json = "{\n" +
                "    \"mobile\": \"17695536722\",\n" +
                "    \"city_name\": \"上海市\",\n" +
                "    \"enterprise_name\": \"kps\",\n" +
                "    \"enterprise_address\": \"上海市虹口区虹口足球场东江湾路\",\n" +
                "    \"contact_name\": dev,\n" +
                "    \"contact_phone\": \"17695536722\",\n" +
                "    \"email\": \"sunyufan@kaopusong.com\"\n" +
                "}";
        AddDadaMerchantTO addDadaMerchantTO = gson.fromJson(json, AddDadaMerchantTO.class);

        System.out.println(addDadaMerchantTO);
        DadaRequestParam<AddDadaMerchantTO> requestParam = new DadaRequestParam<>(addDadaMerchantTO, requestClient.getAppConfig());
        requestParam.setSourceId("");

        DadaApiResponse apiResponse = requestClient.callRpc(requestParam, UrlConstant.MERCHANT_ADD_URL);
        log.info(apiResponse.toString());

    }

    @Test
    public void updateOrder() {
        String json = "{ \n" +
                "     \"signature\": \"aaaaaaaaaaaaaaaaaa\", \n" +
                "     \"client_id\": \"270340107542552\",  \n" +
                "     \"order_id\": \"111111111111\",  \n" +
                "     \"order_status\": 4, \n" +
                "     \"cancel_reason\": \"\", \n" +
                "     \"cancel_from\": 0,  \n" +
                "     \"dm_id\": 1, \n" +
                "     \"dm_name\": \"测试\", \n" +
                "     \"dm_mobile\": \"13100001111\", \n" +
                "     \"finish_code\": \"8888\", \n" +
                "     \"update_time\": 1511789475 \n" +
                "  } ";
        UpdateDadaOrderTO updateDadaOrderTO = gson.fromJson(json, UpdateDadaOrderTO.class);
        System.out.println(updateDadaOrderTO);
        System.out.println(gson.toJson(updateDadaOrderTO));
        switch (updateDadaOrderTO.getOrderStatus()) {
            case 1: {

            }
            default:
                break;
        }
    }

}
